<?php
define("_needlogin", "1");
define("_access", "1");
include_once("include/db.config.php");
/**
 * $totalnum是一共需要做的题目。
 * $single是每题的分数
 */
//var_dump(count($_POST));
$totalnum=50;
$single=5;
/**
 * @param $a
 * @return string
 * 字符串转换函数。用来把答案的大小写同统一；
 */
function conventor($a){
    if(is_string($a)){
        if(ord($a)<=90&&ord($a)>=65){
            return chr(ord($a)+32);
        }else{
            return $a;
        }
    }
}
//查表，看看上次是否做题目，如果没有做完则把上次的题目数以及分数赋给session里面；
if(!isset($_SESSION['mark'])&&!isset($_SESSION['result'])&&!isset($_SESSION['num'])){
$sql6="select radio_num from team where id ={$_SESSION['team']['id']}";
$lastnum=getOne($sql6);
    $_SESSION['num']=$lastnum['radio_num']+1;
    if($_SESSION['num']==$totalnum||$_SESSION['num']==0){
        unset($_SESSION['num']);
    }
    $sql7="select radio_current from team where id = {$_SESSION['team']['id']}";
    $current=getOne($sql7);
    $current=$current['radio_current'];
    if($current!==0){
        $_SESSION['mark']=$current;
    }
}
//溢出操作，如果有溢出，则把各项都清零 ，然后重新做题；
if($_SESSION['num']>$totalnum){
    unset($_SESSION['mark']);
    unset($_SESSION['result']);
    unset($_SESSION['num']);
    $sql="update team set radio_num =0 WHERE  id={$_SESSION['team']['id']}";
    mysql_query($sql);
    $sql8="update team set radio_current = 0 WHERE  id ={$_SESSION['team']['id']}";
    mysql_query($sql8);
    header("location: ./challenge_radio.php");
}
//是否重做的有关判断；
if(isset($_POST['reset'])){
    $sql1="select radio_high from team where id ={$_SESSION['team']['id']}";
    $high=getOne($sql1);
    if($_SESSION['mark']>$high['radio_high']){
        $sql2="update team set radio_high ={$_SESSION['mark']} WHERE id ={$_SESSION['team']['id']}";
        mysql_query($sql2);
        $sql10="select score from team WHERE id ={$_SESSION['team']['id']}";
        $totalscore=getOne($sql10);
        $totalscore=$totalscore['score'];
        $totalscore=$totalscore-$high['radio_high']+$_SESSION['mark'];
        $sql11="update team set score = {$totalscore} WHERE id ={$_SESSION['team']['id']}";
        mysql_query($sql11);
    }
    $sql8="update team set radio_current = 0 WHERE  id ={$_SESSION['team']['id']}";
    mysql_query($sql8);
    unset($_SESSION['mark']);
    unset($_SESSION['result']);
    unset($_SESSION['num']);
    $sql="update team set radio_num =0 WHERE  id={$_SESSION['team']['id']}";
    mysql_query($sql);
}
//取题目的相关操作，把题目以及答案存到session里面，防止因为刷新导致的各种问题；
$sql = 'select * from challenge_radio where enable=1';
$data = getAll($sql);
//var_dump($data);
foreach($data as $ky => $vl){
    $data["$ky"]["answer"]=conventor($vl['answer']);
}
if(count($data)<$totalnum){
    $totalnum=count($data);
}
//var_dump($results);
//$_SESSION['mark']=0;
$nownum=isset($_SESSION['num'])?$totalnum-$_SESSION['num']+1:$totalnum;
if(!isset($_SESSION['result'])){
shuffle($data);
$result = array_slice($data,0,$nownum);
    if(count($result)===$totalnum){
$_SESSION['result']=$result;}else{
        for($i=$_SESSION['num'],$j=0;$i<$totalnum,$j<count($result);$i++,$j++){
            $k=$i-1;
            $_SESSION['result']["$k"]=$result["$j"];
        }
    }
}

//var_dump($_SESSION);
//对于成绩，做题数量，进行初始化操作；
if(!isset($_SESSION['mark'])) {
    $_SESSION['mark'] = 0;
}
if(!isset($_SESSION['num'])||$_SESSION['num']==0) {
    $_SESSION['num'] = 1;
}
//对于提交的处理，并且判断了提交的size；实现加分，插库，等操作；
if(isset($_POST['submit'])&&count($_POST)==2){
    $numm=$_SESSION['num'];
    $numm2=$numm-1;
    foreach($_POST as $key => $valu){
        if($valu===$_SESSION['result']["$numm2"]['answer']){
            $_SESSION['mark']+=$single;
        }
    }
    $sql3="update team set radio_num ={$_SESSION['num']} where id ={$_SESSION['team']['id']}";
    mysql_query($sql3);
    $sql4="select radio_high from team where id ={$_SESSION['team']['id']}";
    $high=getOne($sql4);
    if($_SESSION['mark']>$high['radio_high']){
        $sql5="update team set radio_high ={$_SESSION['mark']} WHERE id ={$_SESSION['team']['id']}";
        mysql_query($sql5);
        $sql10="select score from team WHERE id ={$_SESSION['team']['id']}";
        $totalscore=getOne($sql10);
        $totalscore=$totalscore['score'];
        $totalscore=$totalscore-$high['radio_high']+$_SESSION['mark'];
        $sql11="update team set score = {$totalscore} WHERE id ={$_SESSION['team']['id']}";
        mysql_query($sql11);
    }
    $sql9="update team set radio_current = {$_SESSION['mark']} WHERE id = {$_SESSION['team']['id']}";
    mysql_query($sql9);
    $_SESSION['num']++;
}
    $num=$_SESSION['num'];
//判断是否做完所有题目，做完了$end为真；在模板里面判断
    if($num===$totalnum+1){
        $end=true;
        $sql8="update team set radio_current = 0 WHERE  id ={$_SESSION['team']['id']}";
        mysql_query($sql8);
    }
//题目的提取以及传递到模板；
    $num2=$num-1;
    $results=$_SESSION['result']["$num2"];
//var_dump($_SESSION);
    include "radio.php";
//if(isset($_POST['submit'])){
//    $_SESSION['mark']=0;
//    foreach($res as $key1 => $value1){
//        $id=$value1['id'];
//        if($_POST["$id"]===$value1['answer']){
//            $_SESSION['mark']+=20;
//        }
//    }
////    echo $_SESSION['mark'];
//    include "radio.php";
//}else{
//    include "radio.php";
//}
